package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetmealCheckgroupDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCheckgroup;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: liangWeiQUQ
 * @Description: TODO
 * @DateTime: 2021/2/27 17:17
 **/
@Slf4j
@DubboService
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealCheckgroupDao setmealCheckgroupDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    /**
     * @Author: liangWeiQUQ
     * @Date: 17:18 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: 套餐分页查询
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]data: {}", queryPageBean);
        //1.分页的插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.组装查询条件
        Example example = new Example(Setmeal.class);
        Example.Criteria criteria = example.createCriteria();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("code", "%" + queryString + "%");
            criteria.orLike("name", "%" + queryString + "%");
            criteria.orLike("helpCode", "%" + queryString + "%");
        }
        //3.查询
        Page<Setmeal> setmeals = (Page<Setmeal>) setmealDao.selectByExample(example);
        //4.返回数据
        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }


    /**
     * @Author: liangWeiQUQ
     * @Date: 22:48 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: 套餐管理-添加套餐
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    @Transactional
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        //套餐管理-添加套餐
        setmealDao.insertSelective(setmeal);
        //维护中间关系表
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealCheckgroupDao.insertSelective(setmealCheckgroup);
        }
    }

    /**
     * @Author: liangWeiQUQ
     * @Date: 9:52 2021/3/1
     * @Parms
     * @ReturnType:
     * @Description: 根据filename查询一共有几条记录
     */
    @Override
    public int conut(String fileName) {
        Setmeal setmeal = new Setmeal();
        setmeal.setImg(fileName);
       return setmealDao.selectCount(setmeal);

    }
    /**
             * @Author: 传闻中的梁大侠
             * @Date: 10:12 2021/3/4
             * @Parms []
             * @ReturnType: java.util.List<com.itheima.health.pojo.Setmeal>
             * @Description: 查询所有的套餐列别方法
            */


    @Override
    public List<Setmeal> findAll() {
        return setmealDao.selectAll();
    }
    /**
             * @Author: 传闻中的梁大侠
             * @Date: 10:20 2021/3/4
             * @Parms [id]
             * @ReturnType: com.itheima.health.pojo.Setmeal
             * @Description: 根据id查询套餐的信息
            */

    @Override
    public Setmeal findById(Integer id) {
        //多级别联机查询--根据ID查询
        Setmeal setmeal = setmealDao.selectByPrimaryKey(id);
        //查询检查组
        List<CheckGroup> checkGroupList = checkGroupDao.findBySetmealId(setmeal.getId());
        setmeal.setCheckGroups(checkGroupList);
        //查询检查项目
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItems = checkItemDao.selectByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        return setmeal;
    }
}
